#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// WiFi配置
const char* ssid = "miumiu";
const char* password = "Gznet10086";

// 目标服务器配置
const char* serverIP = "47.107.173.88";  // 替换为你的目标IP
const int serverPort = 8008;             // 替换为你的目标端口
const char* serverURL = "http://yourserver.com/api/data";  // 替换为你的网址

// 按钮引脚定义
#define BUTTON_1 2    // GPIO5 - D1
#define BUTTON_2 1    // GPIO4 - D2  
#define BUTTON_3 0    // GPIO0 - D3

// 按钮状态跟踪
bool lastButtonState1 = HIGH;
bool lastButtonState2 = HIGH;
bool lastButtonState3 = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
 
  Serial.begin(9600);

  // 初始化按钮引脚（启用内部上拉电阻）
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);
  pinMode(BUTTON_3, INPUT_PULLUP);

  // 连接WiFi
  connectToWiFi();

  Serial.println("press...");
  Serial.println("key 1 send");
  Serial.println("key 2 send");
  Serial.println("key 3 send");
}

void loop() {
  // 读取当前按钮状态
  bool currentState1 = digitalRead(BUTTON_1);
  bool currentState2 = digitalRead(BUTTON_2);
  bool currentState3 = digitalRead(BUTTON_3);

  // 检查按钮1是否被按下（低电平）
  if (currentState1 == LOW && lastButtonState1 == HIGH) {
    delay(debounceDelay); // 防抖延迟
    if (digitalRead(BUTTON_1) == LOW) {
      Serial.println("key 1");
      sendData(1);
    }
  }

  // 检查按钮2是否被按下
  if (currentState2 == LOW && lastButtonState2 == HIGH) {
    delay(debounceDelay);
    if (digitalRead(BUTTON_2) == LOW) {
      Serial.println("key 2");
      sendData(2);
    }
  }

  // 检查按钮3是否被按下
  if (currentState3 == LOW && lastButtonState3 == HIGH) {
    delay(debounceDelay);
    if (digitalRead(BUTTON_3) == LOW) {
      Serial.println("key 3");
      sendData(3);
    }
  }

  // 更新上一次的按钮状态
  lastButtonState1 = currentState1;
  lastButtonState2 = currentState2;
  lastButtonState3 = currentState3;

  delay(100); // 主循环延迟
 

}

// 连接到WiFi网络
void connectToWiFi() {
  Serial.print("link WiFi: ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi success!");
  Serial.print("IPaddress: ");
  Serial.println(WiFi.localIP());
}

// 发送数据函数
void sendData(int buttonNumber) {
  String dataToSend = "";

  // 根据按钮编号确定要发送的数据
  switch (buttonNumber) {
    case 1:
      dataToSend = "BUTTON_1_PRESSED";
      break;
    case 2:
      dataToSend = "BUTTON_2_PRESSED";
      break;
    case 3:
      dataToSend = "BUTTON_3_PRESSED";
      break;
    default:
      dataToSend = "UNKNOWN_BUTTON";
  }

  // 选择发送方式（取消注释你想要使用的方式）

  // 方式1：使用TCP发送
  sendViaTCP(dataToSend);
 
}

// 🚀 方式1：通过TCP发送数据
void sendViaTCP(String data) {
  Serial.println("TCP send...");

  WiFiClient client;

  if (!client.connect(serverIP, serverPort)) {
    Serial.println("TCP linik fail!");
    return;
  }

  Serial.print("send TC data: ");
  Serial.println(data);

  client.println(data);
  client.println(); // 发送空行表示结束

  // 等待响应
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println("TCP out time!");
      client.stop();
      return;
    }
  }

  // 读取并打印响应
  Serial.print("sever: ");
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  Serial.println();

  client.stop();
}
